# -*- coding: utf-8 -*- 

#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
## - call exposes all registered services (none by default)
#########################################################################  

from applications.web2pyorm.modules.orm import *
from applications.web2pyorm.modules.dao import *

def index():
    """
    Something similar to Hibernate for web2py
    """
   
    person_dao = PersonDao(db)
    
    """ This is an empty person """
    empty_person = person_dao.dto
    """ Alternative """
    ## empty_person = PersonDao(db).dto
    
    empty_person.surname.value = 'Elena'
    persons = person_dao.findByExample(empty_person)
    
    """ This is the person from database with the id = 1 """
    person = person_dao.findById(1)
    """ Another person """
    ## another_person = person_dao.findById(3)
    
    """ This update the person in database """
    person.surname.value = 'Johnny'; person.name.value = 'Mnemonic'; person.language_id.value = 1
    person_dao.update(person)

    """
    Some candy
    This will create an inflated DTO from nested DTOs
    """
    forth_person = PersonDao(db).findByIdRecursive(1).inflate()
    
    """ To specify the members (fields) from table structure """
    ## pers_dao = PersonDao(db, ['id', 'name'])
    
    """ Add a person into database """
    a_person = PersonDao(db).dto
    a_person['surname'] = 'Elena';
    a_person['name'] = 'Gheorghe'
    a_person['language_id'] = 3
    id = person_dao.persist(a_person)
    
    """ Remove a person by id """
    person_dao.removeById(id)
    
    """ All persons from database """
    ## all_persons = person_dao.query(db.person.id > 0)
        
    response.flash = T('Welcome to web2py')
    
    return dict(message=T('Hello World'), person = person, forth_person = forth_person, persons = persons)

def user():
    """
    exposes:
    http://..../[app]/default/user/login 
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """
    return dict(form=auth())


def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request,db)


def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    session.forget()
    return service()


